package com.tuoyu.peopletest.utils;


import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.util.TypedValue;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.tuoyu.peopletest.common.MyApplication;

import java.lang.reflect.Field;

import androidx.core.content.ContextCompat;


/**
 * 项目关联工具类,  比如: 通用UI逻辑
 */
public class SCUIUtils {

    private static Typeface mIconfont;

    public static Context getContext() {
        return MyApplication.mInstence.context;
    }

    //获取主线程的handler对象
    public static Handler getMainThreadHandler() {
        return MyApplication.mInstence.mHandler;
    }

    //获取主线程的线程id
    public static int getMainThreadId() {
        return MyApplication.mInstence.mainThreadId;
    }

    //获取字符串资源
    public static String getString(int resId) {
        return getContext().getResources().getString(resId);
    }


    //获取字符串数组
    public static String[] getStringArray(int resId) {
        return getContext().getResources().getStringArray(resId);
    }

    //获取Drawable
    public static Drawable getDrawable(int resId) {
        return getContext().getResources().getDrawable(resId);

    }

    //获取color
    public static int getColor(int resId) {

        //return getContext().getResources().getColor(resId);
        return ContextCompat.getColor(getContext(),resId);
    }

    //获取颜色的状态选择器
    public static ColorStateList getColorStateList(int resId) {
        // TODO Auto-generated method stub
        return getContext().getResources().getColorStateList(resId);
    }

    //获取dimen
    public static int getDimen(int resId) {
        return (int) getContext().getResources().getDimension(resId);
    }

    public static float getDimen2(int resId) {
        return getContext().getResources().getDimension(resId);
    }

    public static int getDimenPixelSize(int resId) {
        return getContext().getResources().getDimensionPixelSize(resId);
    }

    //Android中能操作ui的线程是主线程
    public static boolean isRunOnUiThread() {
        //1、获取当前线程id
        int currentThreadId = android.os.Process.myTid();
        //2、获取主线程的id
        int mainThreadId = getMainThreadId();
        //3、比较
        return currentThreadId == mainThreadId;
    }


    //Thread:线程
    //Runnable：任务

    //保证某一些操作一定运行在主线程中
    public static void runOnUiThread(Runnable r) {

        if (isRunOnUiThread()) {
            //主线程
            //new Thread(r).start();
            r.run();
        } else {
            //把r丢到主线程的消息队列
            getMainThreadHandler().post(r);
        }

    }


    public static int getScreenWidth() {
        return getContext().getResources().getDisplayMetrics().widthPixels;
    }

    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight() {
        return getContext().getResources().getDisplayMetrics().heightPixels;
    }

    //运行在后台子线程
    public static void runOnBackgroundThread(Runnable r) {
        new Thread(r).start();
    }

    /**
     * 设置状态栏字体颜色
     * 设置状态栏是否为浅色模式
     *
     * @param window
     * @param isLightMode
     */
    public static void setStatusBarLightMode(Window window,
                                             boolean isLightMode) {

        //此方法不管用, 在这里集体替换
        BarUtils.setStatusBarLightMode(window,isLightMode);
        //小米手机, 魅族手机
    }

    public static void setStatusBarColor(Window window,int res) {

        //此方法不管用, 在这里集体替换
        BarUtils.setStatusBarColor(window,getColor(res));
    }


    public static void setTextStr(TextView tv, Object object){
        if (object instanceof Integer) {
            tv.setText((Integer) object);
        } else if (object instanceof String) {
            tv.setText((String) object);
        }
    }

    public static void setTextHintStr(TextView tv, Object object){
        if (object instanceof Integer) {
            tv.setHint((Integer) object);
        } else if (object instanceof String) {
            tv.setHint((String) object);
        }
    }

    /**
     * dp转px
     * @param context
     * @param dpValue
     * @return
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * px转dp
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dip(Context context,int pxValue) {
        return ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pxValue, context.getResources().getDisplayMetrics()));
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     * DisplayMetrics类中属性scaledDensity
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 字体图标
     * @return
     */
    public static Typeface getIconTypeface(){
        if(mIconfont ==null){
            mIconfont = Typeface.createFromAsset(getContext().getAssets(), "iconfont.ttf");
        }
        return mIconfont;
    }

    /**
     * 设置picker之间的间距
     */
    public static void setPickerMargin(NumberPicker picker) {
        LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) picker.getLayoutParams();
        p.setMargins(0, 0, 100, 0);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
            p.setMarginStart(0);
            p.setMarginEnd(100);
        }
    }

    /**
     * 设置picker分割线的颜色
     */
    public static void setDividerColor(NumberPicker picker) {
        Field field = null;
        try {
            field = NumberPicker.class.getDeclaredField("mSelectionDivider");
            if (field != null) {
                field.setAccessible(true);
                field.set(picker, new ColorDrawable(Color.RED));
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置picker分割线的宽度(分割线的粗细)
     */
    public static void setNumberPickerDivider(NumberPicker picker) {
        Field[] fields = NumberPicker.class.getDeclaredFields();
        for (Field f : fields) {
            if (f.getName().equals("mSelectionDividerHeight")) {
                f.setAccessible(true);
                try {
                    f.set(picker, 1);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }


}
